package com.xintonghua.base.utils;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.R;
import com.xintonghua.base.adapter.BaseAdapter;
import com.xintonghua.base.bean.DataList;
import com.xintonghua.base.ui.MySubscriber;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 作者：wanxianyi
 * 时间：2018/1/13.
 * 包名：com.xintonghua.base.utils
 */

public class RefreshUtils {
    /**
     * 设置listView区分线
     *
     * @param context
     * @param recyclerView
     */
    public static void initList(Context context, XRecyclerView recyclerView) {
        recyclerView.addItemDecoration(new DividerItemDecoration(context));
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallClipRotatePulse);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
    }

    /**
     * 设置listView区分线
     *
     * @param context
     * @param recyclerView
     * @param height       高
     * @param color        颜色
     */
    public static void initList(Context context, XRecyclerView recyclerView, int height, int color) {
        recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL, height, ContextCompat.getColor(context, color)));
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallClipRotatePulse);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
    }

    public static void initGrid(Context context, XRecyclerView recyclerView) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallClipRotatePulse);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
    }


    /**
     * 添加head
     *
     * @param context
     * @param recyclerView
     * @param layout
     */
    public static void initList(Context context, XRecyclerView recyclerView, int layout) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallClipRotatePulse);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        View view = LayoutInflater.from(context).inflate(layout, recyclerView, false);
        recyclerView.addHeaderView(view);
    }

    public static void onRefresh(final BaseAdapter adapter, final XRecyclerView recyclerView) {
        Flowable.timer(300, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new MySubscriber<Long>() {
            @Override
            public void next(Long aLong) {
                adapter.notifyDataSetChanged();
                recyclerView.refreshComplete();
            }
        });
    }

    public static boolean hasNext(DataList dataList, final BaseAdapter adapter, final XRecyclerView recyclerView) {
        if (dataList.isLastPage()) {

            Flowable.timer(300, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new MySubscriber<Long>() {
                @Override
                public void next(Long aLong) {
                    adapter.notifyDataSetChanged();
                    recyclerView.loadMoreComplete();
                    recyclerView.setFootViewText("加载更多...","没有更多数据");
                    recyclerView.setNoMore(true);

                }
            });
            return false;
        } else {
            return true;
        }

    }
}
